# -*- coding: utf-8 -*-
import scrapy
import json
from ..items import DoubanbookItem
import random

class BookSpider(scrapy.Spider):
    name = 'book'
    allowed_domains = ['book.douban.com']
    start_urls = ['https://book.douban.com/ithil_j/activity/book_annual2020?with_widgets=1']

    def parse(self, response):
        html = json.loads(response.text)
        item = DoubanbookItem()
        #start = html['res']['widgets'][1]['subjects']
        #end = html['res']['widgets'][17]['subjects']
        book_list = html['res']['widgets'][1]['subjects']
        for book in book_list:
            item['book_name'] = book['title']
            item['author'] = book['short_info']
            item['rating'] = book['rating']
            item['rating_count'] = book['rating_count']
            yield item